//
//  DebugLayer.cpp
//  Prism
//
//  Created by Bas du Pre on 25-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "global.h"

#include "graphics/DebugLayer.h"
#include "graphics/Director.h"

using namespace Prism;
using namespace Prism::Graphics;

DebugLayer::DebugLayer() :
_dt(PRISM_DEBUGLAYER_UPDATE)
{
    Size win = theDirector()->winSize();
    
    _fps = new Label("FPS: 0", "Verdana", 12, Color::White, Label::QualitySpeed);
    _fps->color().setAlpha(190);
    _fps->setPosition(2, win.height - 2);
    _fps->setAnchorPoint(0, 1);
    
    _status = new Label("All systems up", "Verdana", 10, Color::White, Label::QualitySpeed);
    _status->color().setAlpha(190);
    _status->setPosition(2, win.height - 22);
    _status->setAnchorPoint(0, 1);
    
    _banner = new Label(String("Prism ") + PRISM_VERSIONSTRING, "Verdana", 12, Color::White, Label::QualityHigh);
    _banner->color().setAlpha(190);
    _banner->setPosition(win.width - 2, win.height - 2);
    _banner->setAnchorPoint(1, 1);
    
    addChild(_fps);
    addChild(_status);
    addChild(_banner);
    
    onUpdate += delegate(this, &DebugLayer::update);
    theDirector()->onWinSizeChanged += delegate(this, &DebugLayer::winSizeChanged);
}

DebugLayer::~DebugLayer() {
    delete _fps;
    delete _status;
    delete _banner;
}

void DebugLayer::update(void* sender, UpdateEventArgs* e) {
    _dt += e->delta;
    if (_dt >= PRISM_DEBUGLAYER_UPDATE) {
        _dt = 0;
        char fps[6];
        sprintf(fps, "%.2f", theDirector()->fps());
        _fps->setValue(String("FPS: ") + String(fps));
        _status->setValue(Logger::instance()->lastMessage());
    }
}

void DebugLayer::winSizeChanged(void *sender, Prism::EventArgs *e) {
    Size win = theDirector()->winSize();
    _fps->setPosition(2, win.height - 2);
    _status->setPosition(2, win.height - 22);
    _banner->setPosition(win.width - 2, win.height - 2);
}
